from pymongo import MongoClient

# 连接到MongoDB，默认连接到本地数据库
client = MongoClient()

# 或者连接到远程数据库
# client = MongoClient('mongodb://username:password@localhost:27017/')
client = MongoClient('mongodb://localhost:27017/')

# 选择数据库
db = client['game']

# 选择集合
collection = db['mycollection']

# # 插入单个文档
# collection.insert_one({'name': 'Alice', 'age': 25})
#
# # 插入多个文档
# collection.insert_many([{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}])

# 查询所有文档
# for doc in collection.find():
#     print(doc)

# 查询单个文档
# print(collection.find_one({'name': 'Alice'}))

# 模糊查询
results = collection.find({'name': {'$regex': '^A'}})
for result in results:
    print(result)



